$(document).ready(function () {
    window.setInterval(watchToken, 60000 * 20);
});

function watchToken() {
    var token = localStorage.getItem("token");
    var refreshToken = localStorage.getItem("refreshToken");
    var expireTime = localStorage.getItem("expireTime");
    //alert(token)
    //当前时间
    var nowDate = new Date();
    var year = nowDate.getFullYear();
    var month = nowDate.getMonth() + 1 < 10 ? "0" + (nowDate.getMonth() + 1) : nowDate.getMonth() + 1;
    var date = nowDate.getDate() < 10 ? "0" + nowDate.getDate() : nowDate.getDate();
    var hour = nowDate.getHours()< 10 ? "0" + nowDate.getHours() : nowDate.getHours();
    var minute = nowDate.getMinutes()< 10 ? "0" + nowDate.getMinutes() : nowDate.getMinutes();
    var second = nowDate.getSeconds()< 10 ? "0" + nowDate.getSeconds() : nowDate.getSeconds();
    var now = year + "-" + month + "-" + date+" "+hour+":" + minute + ":" + second;
    $.ajax({
        type:"post",
        url: basePath + "token/refresh",
        data:{
            refreshToken : refreshToken,
            expireTime: expireTime
        },
        async:false,
        cache:true,
        dataType: "json", //this is important
        success:function(result) {
            localStorage.setItem("token",result.token);
            localStorage.setItem("refreshToken",result.refreshToken);
            localStorage.setItem("expireTime",result.expireTime);
            localStorage.setItem("logout","false");
        },
        error : function(result) {
            alert("error:" + result);
        }
    });
}